//
//  MyThingsViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 8/31/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "MyThingsViewController.h"
#import "ThingDetailsViewController.h"
#import "Smarthings.h"
#import <MBProgressHUD.h>

@interface MyThingsViewController () <SmarthingsDelegate>

@property(nonatomic, strong) NSArray *things;
@property(nonatomic, strong) Smarthings *smarthingsSDK;

- (void)refreshScreen;

@end

@implementation MyThingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.smarthingsSDK = [Smarthings new];
    self.smarthingsSDK.delegate = self;
}

- (IBAction)refreshButtonTapped:(id)sender {
    [self refreshScreen];
}

- (void)viewWillAppear:(BOOL)animated {
    [self refreshScreen];
}

- (void)refreshScreen {
    [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
    [self.smarthingsSDK getThingsList];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.things.count;
    }
    else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThingCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ThingCell"];
    }
    
    SMTThing * thing = [self.things objectAtIndex:indexPath.row];
    cell.textLabel.text = thing.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu properties, %lu actions", (unsigned long)thing.properties.count, (unsigned long)thing.actions.count];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
        [self.smarthingsSDK deleteThing:self.things[indexPath.row]];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"openThing"]) {
        [(ThingDetailsViewController *)[segue destinationViewController] setThing:[self.things objectAtIndex:self.tableView.indexPathForSelectedRow.row]];
        [(ThingDetailsViewController *)[segue destinationViewController] setIsNewThing:NO];
    }
    else if ([segue.identifier isEqualToString:@"newThing"]) {
        SMTThing *thing = [[SMTThing alloc] init];
        thing.name = @"Untitled Thing";
        thing.properties = @[];
        thing.actions = @[];
        [(ThingDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setThing:thing];
        [(ThingDetailsViewController *)[(UINavigationController *)[segue destinationViewController] topViewController] setIsNewThing:YES];
    }

}

#pragma mark - Smarthings Delegate

- (void)getThingsListFinishedWithFailure:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.tabBarController.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Things"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}

- (void)getThingsListFinishedWithSuccess:(NSArray *)things {
    [MBProgressHUD hideHUDForView:self.tabBarController.view animated:YES];
    self.things = things;
    [self.tableView reloadData];
}

- (void)deleteThingFinishedWithFailure:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.tabBarController.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Unregistering Thing"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}

- (void)deleteThingFinishedWithSuccess:(NSString *)href {
    [self.smarthingsSDK getThingsList];
}


@end
